<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/23
 * Time: 13:42
 */
namespace app\admin\model;
use think\Db;
use think\Exception;

class MyBackups{
    protected $list = [];
    protected $sql = [];
    protected $file = '';
    public function setFile($file=null,$change=[]){
        if($file&&is_file($file)){
            $this->file = $file;
            $str = file_get_contents($file);
            foreach ($change as $key=>$value){
                $str = preg_replace($key,$value,$str);
            }
            $this->sql = explode(';',$str);
        }
        return $this;
    }
    public function getFiel(){
        if(!is_file($this->file)){
            throw new Exception('文件不存在');
        }
        return $this->file;
    }
    public function huanyuan($file,$change=[]){
        $this->setFile($file,$change);
        try{
            foreach ($this->sql as $value){
                $value = trim($value);
                if($value){
                    Db::execute($value);
                }
            }
        }catch (Exception $e){
            return false;
        }
    }
}